//#define __DEBUG__
// by Xeno
#define THIS_FILE "x_m107.sqf"
#include "x_setup.sqf"
private ["_vehicle", "_poss", "_ogroup", "_unit", "_officer", "_endtime"];

_mpos = markerPos QGVAR(sm_107);
_mpos set [2,0];
GVAR(x_sm_pos) = [_mpos]; //  destroy scud
GVAR(x_sm_type) = "normal"; // "convoy"

#ifdef __SMMISSIONS_MARKER__
if (true) exitWith {};
#endif

if (X_Client && {!GVAR(IS_HC_CLIENT)}) then {
	GVAR(cur_sm_txt) = (localize "STR_DOM_SIDESTRING_1071");
	GVAR(current_mission_resolved_text) = (localize "STR_DOM_SIDESTRING_1071");
};

if (call FUNC(checkSHC)) then {
	_MissionCompleted = {
	#ifndef __TT__
		GVAR(sm_winner) = 2;
	#else
		if (GVAR(sm_points_blufor) > GVAR(sm_points_opfor)) then {
			GVAR(sm_winner) = 2;
		} else {
			if (GVAR(sm_points_opfor) > GVAR(sm_points_blufor)) then {
				GVAR(sm_winner) = 1;
			} else {
				if (GVAR(sm_points_opfor) == GVAR(sm_points_blufor)) then {
					GVAR(sm_winner) = 123;
				};
			};
		};
	#endif
		GVAR(sm_resolved) = true;
		if (GVAR(IS_HC_CLIENT)) then {
			[QGVAR(sm_var), GVAR(sm_winner)] call FUNC(NetCallEventCTS);
		};
	};

	__PossAndOther
	// A3: Check if A3 still has SCUD
	_vehicle = createVehicle [GVAR(sm_SCUD), _poss, [], 0, "NONE"];
	_vehicle setDir (markerDir QGVAR(sm_107));
	_vehicle setPos _poss;
	_vehicle setFuel 0;
	#ifdef __TT__
	_vehicle addEventHandler ["killed", {_this call FUNC(AddSMPoints)}];
	#endif
	__GetEGrp(_ogroup)
	_unit = _ogroup createUnit [GVAR(sm_simple_soldier_opfor), _poss, [], 0, "FORM"];
	if (GVAR(without_nvg) == 0) then {
		_unit call FUNC(removeNVGoggles);
	};
	_unit setVariable ["BIS_noCoreConversations", true];
	if (GVAR(domdatabase)) then {
		_unit addEventHandler ["killed", {if (isPlayer (_this select 1)) then {[QGVAR(PAIKP), _this select 1] call FUNC(NetCallEventCTS)}}];
	};
	_unit moveInDriver _vehicle;
	__AddToExtraVec(_unit)
	__AddToExtraVec(_vehicle)
	if (GVAR(domdatabase)) then {
		_vehicle addMPEventHandler ["MPKilled", {if (isServer && {isPlayer (_this select 1)}) then {(_this select 1) call FUNC(PAddCarKillPoints)}}}];
	};
	sleep 2.123;
	["specops", 2, "basic", 4, _poss,100,true] spawn FUNC(CreateInf);
	sleep 2.321;
	["shilka", 1, "bmp", 1, "tank", 0, _poss,1,250,true] spawn FUNC(CreateArmor);
	
	_endtime = time + 900 + random 100;
	waitUntil {sleep 0.321;!alive _vehicle || {time > _endtime}};
	if (alive _vehicle) then {
		[QGVAR(smsg)] call FUNC(NetCallEventToClients);
		_vehicle action ["ScudLaunch",_vehicle];
	};
	sleep 30;
	if (alive _vehicle) then {
		_vehicle action ["ScudStart",_vehicle];
		GVAR(sm_winner) = -879;
		GVAR(sm_resolved) = true;
		if (GVAR(IS_HC_CLIENT)) then {
			[QGVAR(sm_var), GVAR(sm_winner)] call FUNC(NetCallEventCTS);
		};
	} else {
		call _MissionCompleted;
	};
};
